//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ForwardSwapQuote.h"
using namespace Cephei::QL::Quotes;
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (Cephei::QL::Indexes::ISwapIndex^ swapIndex, Cephei::QL::IQuote^ spread, Cephei::QL::Times::IPeriod^ fwdStart) : CQuote(CForwardSwapQuote::typeid)
{
    CSwapIndex^ _CswapIndex;
    CQuote^ _Cspread;
    CPeriod^ _CfwdStart;
    try
    {
#ifdef HANDLE
        _phForwardSwapQuote = NULL;
#endif
        _CswapIndex = safe_cast<CSwapIndex^> (swapIndex);
        _CswapIndex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _swapIndex = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_CswapIndex->GetShared ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _CfwdStart = safe_cast<CPeriod^> (fwdStart);
        _CfwdStart->Lock();
        QuantLib::Period& _fwdStart = static_cast<QuantLib::Period&> (_CfwdStart->GetReference ()); 
        _ppForwardSwapQuote = new boost::shared_ptr<QuantLib::ForwardSwapQuote> (new QuantLib::ForwardSwapQuote ( _swapIndex,  _spread,  _fwdStart ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CswapIndex != nullptr) _CswapIndex->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
        if (_CfwdStart != nullptr) _CfwdStart->Unlock();
    }
}
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (boost::shared_ptr<QuantLib::ForwardSwapQuote>& childNative, Object^ owner) : CQuote(CForwardSwapQuote::typeid)
{
#ifdef HANDLE
	_phForwardSwapQuote = NULL;
#endif
	_ppForwardSwapQuote = &childNative;
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
}
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (QuantLib::ForwardSwapQuote& childNative, Object^ owner) : CQuote(CForwardSwapQuote::typeid)
{
#ifdef HANDLE
	_phForwardSwapQuote = NULL;
#endif
	_ppForwardSwapQuote = new boost::shared_ptr<QuantLib::ForwardSwapQuote> (&childNative);
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
    _ForwardSwapQuoteOwner = owner;
    _QuoteOwner = owner;
}

Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (CForwardSwapQuote^ copy) : CQuote(CForwardSwapQuote::typeid)
{
#ifdef HANDLE
	_phForwardSwapQuote = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForwardSwapQuote = new boost::shared_ptr<QuantLib::ForwardSwapQuote> (copy->GetShared());
        _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
    }
}
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (System::Type^ t) : CQuote(CForwardSwapQuote::typeid)
{
#ifdef HANDLE
	_phForwardSwapQuote = NULL;
#endif
	if (!t->IsSubclassOf(CForwardSwapQuote::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (QuantLib::Handle<QuantLib::ForwardSwapQuote>& childNative, Object^ owner)  : CQuote(CForwardSwapQuote::typeid)
{
	_phForwardSwapQuote = &childNative;
	_ppForwardSwapQuote = &static_cast<boost::shared_ptr<QuantLib::ForwardSwapQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
    _ForwardSwapQuoteOwner = owner;
}
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (QuantLib::Handle<QuantLib::ForwardSwapQuote> childNative)  : CQuote(CForwardSwapQuote::typeid)
{
	_phForwardSwapQuote = &childNative;
	_ppForwardSwapQuote = &static_cast<boost::shared_ptr<QuantLib::ForwardSwapQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
}
#endif
#ifdef STRUCT
Cephei::QL::Quotes::CForwardSwapQuote::CForwardSwapQuote (QuantLib::ForwardSwapQuote childNative)  : CQuote(CForwardSwapQuote::typeid)
{
#ifdef HANDLE
	_phForwardSwapQuote = NULL;
#endif
	_ppForwardSwapQuote = new boost::shared_ptr<QuantLib::ForwardSwapQuote> (new QuantLib::ForwardSwapQuote (childNative));
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppForwardSwapQuote));
}
#endif

Cephei::QL::Quotes::CForwardSwapQuote::~CForwardSwapQuote ()
{
    if (_ppForwardSwapQuote != NULL)
    {
	    delete _ppForwardSwapQuote;
        _ppForwardSwapQuote = NULL;
    }
}
Cephei::QL::Quotes::CForwardSwapQuote::!CForwardSwapQuote ()
{
    if (_ppForwardSwapQuote != NULL)
    {
	    delete _ppForwardSwapQuote;
    }
}
QuantLib::ForwardSwapQuote& Cephei::QL::Quotes::CForwardSwapQuote::GetReference ()
{
    if (_ppForwardSwapQuote == NULL) throw gcnew NativeNullException ();
	return **_ppForwardSwapQuote;
}
boost::shared_ptr<QuantLib::ForwardSwapQuote>& Cephei::QL::Quotes::CForwardSwapQuote::GetShared ()
{
    if (_ppForwardSwapQuote == NULL) throw gcnew NativeNullException ();
	return *_ppForwardSwapQuote;
}
QuantLib::ForwardSwapQuote* Cephei::QL::Quotes::CForwardSwapQuote::GetPointer ()
{
    if (_ppForwardSwapQuote == NULL) throw gcnew NativeNullException ();
	return &**_ppForwardSwapQuote;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ForwardSwapQuote>& Cephei::QL::Quotes::CForwardSwapQuote::GetHandle ()
{
	if (_phForwardSwapQuote == NULL)
	{
		_phForwardSwapQuote = new Handle<QuantLib::ForwardSwapQuote> (*_ppForwardSwapQuote);
	}
	return *_phForwardSwapQuote;
}
#endif
bool Cephei::QL::Quotes::CForwardSwapQuote::HasNative () 
{
	return (_ppForwardSwapQuote != NULL);
}

DateTime Cephei::QL::Quotes::CForwardSwapQuote::FixingDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardSwapQuote)->fixingDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Quotes::CForwardSwapQuote::IsValid::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppForwardSwapQuote)->isValid ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Quotes::CForwardSwapQuote::StartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardSwapQuote)->startDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Quotes::IForwardSwapQuote^ Cephei::QL::Quotes::CForwardSwapQuote::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppForwardSwapQuote)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Quotes::CForwardSwapQuote::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppForwardSwapQuote)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Quotes::CForwardSwapQuote::ValueDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardSwapQuote)->valueDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Quotes::IForwardSwapQuote^ Cephei::QL::Quotes::CForwardSwapQuote_Factory::Create (Cephei::QL::Indexes::ISwapIndex^ swapIndex, Cephei::QL::IQuote^ spread, Cephei::QL::Times::IPeriod^ fwdStart)
{
    return gcnew CForwardSwapQuote ( swapIndex,  spread,  fwdStart);
}
